package dao.impl;

import dao.StaffsDao;
import entity.StaffsEntity;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @AUTHOR CJ
 * <p>
 * *@DATE 2025 - 02 - 24
 */
public class StaffsDaoImpl implements StaffsDao {



     QueryRunner qr = new QueryRunner(DbUtil.getDataSource());
    @Override
    public int insert(StaffsEntity staff) {
        String sql = "INSERT INTO staffs(name, age, phone, sta_pos) VALUES (?, ?, ?, ?)";
        Object[] params = {staff.getName(), staff.getAge(), staff.getPhone(), staff.getStaPos()};

        try {
            return qr.update(DbUtil.getConnection(), sql, params);
        } catch (SQLException e) {
            e.printStackTrace(); // 打印异常信息，便于调试
            throw new RuntimeException("Failed to insert staff", e); // 抛出运行时异常，便于上层捕获
        }
    }


    public static void main(String[] args) {
        StaffsDaoImpl dao = new StaffsDaoImpl();
//      StaffsEntity staf = new StaffsEntity();
//       staf.setName("小翔");
//       staf.setAge(20);
//      staf.setPhone("18153479492");
//      staf.setStaPos("教授");
        System.out.print(dao.selectStaffs());

//        staf.setId(4);
        //System.out.println(dao.updateStaffs(staf));
    }
    @Override
    public int insertStaffs(StaffsEntity staffs) {
            int count = 0;
//            String sql = "insert into staffs(name,age,phone,sta_pos) values (?,?,?,?)";
//            PreparedStatement ps = null;
//        try {
//            ps = conn.prepareStatement(sql);
//            ps.setString(1, staffs.getName());
//            ps.setInt(2,staffs.getAge());
//            ps.setString(3,staffs.getPhone());
//            ps.setString(4,staffs.getStaPos());
//            count = ps.executeUpdate();
//            if(count == 1){
//                System.out.print("添加成功");
//            }else{
//                System.out.print("添加失败");
//            }
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
        return count;
    }

    @Override
    public int updateStaffs(StaffsEntity staffs) {
//        int count = 0;
//        String sql = " update `staffs` set name = ?,age = ?,phone = ?,sta_pos = ? where id = ?";
//        PreparedStatement ps = null;
//
//        try {
//            ps = conn.prepareStatement(sql);
//            ps.setString(1, staffs.getName());
//            ps.setInt(2,staffs.getAge());
//            ps.setString(3,staffs.getPhone());
//            ps.setString(4,staffs.getStaPos());
//            ps.setInt(5,staffs.getId());
//            count = ps.executeUpdate();
//            if(count == 1){
//                System.out.print("添加成功");
//            }else{
//                System.out.print("添加失败");
//            }
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }finally {
//            DbUtil.close(null,ps,conn);
//        }
        return -1;
    }


    public int deleteStaffs(int staffsId) {
//        int count = 0;
//        String sql = "delete from `staffs` where id = ?";
//        PreparedStatement ps = null;
//        try {
//            ps = conn.prepareStatement(sql);
//            ps.setInt(1, staffsId);
//            count = ps.executeUpdate();
//            if(count == 1){
//                System.out.print("添加成功");
//            }else{
//                System.out.print("添加失败");
//            }
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }finally {
//            DbUtil.close(null,ps,conn);
//        }
        return -1;
    }

        public List<StaffsEntity> PaginationQuery(int currentPage, int pageSize) {
//            List<StaffsEntity> list = new ArrayList<>();
//            int offset = (currentPage - 1) * pageSize;
//
//            String sql = "SELECT * FROM staffs LIMIT ? OFFSET ?";
//            PreparedStatement ps = null;
//            ResultSet re = null;
//
//            try {
//                ps = conn.prepareStatement(sql);
//                ps.setInt(1, pageSize);
//                ps.setInt(2, offset);
//                re = ps.executeQuery();
//
//                while (re.next()) {
//                    StaffsEntity staf = new StaffsEntity(
//                            re.getInt(1),
//                            re.getString(2),
//                            re.getInt(3),
//                            re.getString(4),
//                            re.getString(5),
//                            re.gr(6),
//                            re.getDate(7)
//                    );
//                    list.add(staf);
//                }
//            } catch (SQLException e) {
//                throw new RuntimeException(e);
//            } finally {
//                DbUtil.close(re, ps, null); // 注意这里不需要关闭 conn，因为它在类级别定义
//            }
           return null;
        }

    @Override
    public List<StaffsEntity> selectByPage(int correntPage, int pageSize) {
        String sql = "select * from staffs limit ?,?";
        int limit = (correntPage - 1) * pageSize;
        BeanListHandler<StaffsEntity> beanListHandler = new BeanListHandler<>(StaffsEntity.class);

        try {
            return qr.query(sql, beanListHandler, limit, pageSize);
        } catch (SQLException e) {
            throw new RuntimeException("分页查询异常", e);
        }
    }

    public StaffsEntity selectStaffs() {
        String sql = "select id,name,age,phone,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs" ;
        try {
            return qr.query(sql,new BeanHandler<>(StaffsEntity.class));
        } catch (SQLException e) {
            throw new RuntimeException("根据ID查询异常",e);
        }
    }
}
